import ControlPad from "../gamecontrol/ControlPad";

export default class Game extends Laya.Script {

    private target: Laya.Sprite;
    private lblKeyStatus: Laya.Label;
    private controlPad: ControlPad;
    speed: number = 2;

    onAwake() {
        this.target = this.owner.getChildByName("target") as Laya.Sprite;
        this.lblKeyStatus = this.owner.getChildByName("lblKeyStatus") as Laya.Label;
        this.controlPad = this.owner.getComponent(ControlPad);
    }

    onUpdate() {
        if (this.controlPad.direction != -4) {
            console.log(this.controlPad.direction);
            var pi4 = 0.7853981633974483;
            if (this.controlPad.direction < -(pi4 * 3) || this.controlPad.direction > (pi4 * 3)) {
                //方向向左
                this.target.x -= this.speed;
            }
            else if (this.controlPad.direction > (pi4) && this.controlPad.direction < (pi4 * 3)) {
                //方向向下
                this.target.y += this.speed;
            }
            else if (this.controlPad.direction > -pi4 && this.controlPad.direction < pi4) {
                //方向向右
                this.target.x += this.speed;
            } else {
                //方向向上
                this.target.y -= this.speed;
            }
        } else {
        }
        if (this.controlPad.key1Pressed) {
            this.lblKeyStatus.text = "Pressed key1";
            console.log("Pressed key1");
        }
    }
}